﻿<template>
	<view class="shzy-wrap shzy-wrap-form">
		<u-form :model="dataForm" :rules="rules" ref="dataForm" :errorType="['toast']" label-position="right" label-width="100" label-align="left" class="shzy-form">
			<u-form-item label="年度" prop="year" required >
				<u-input v-model="dataForm.year" maxlength='4' placeholder='请输入' ></u-input>
			</u-form-item>
			<u-form-item label="有效标志" prop="enabledMark" >
                <view class="u-flex u-form-item-switch">
					<shzy-switch v-model="dataForm.enabledMark" ></shzy-switch>
                </view>
            </u-form-item>
		</u-form>
		<view class="buttom-actions" v-if="jurisdictionType != 'btn_detail'">
			<u-button class="buttom-btn" @click="shzy.goBack()">取 消</u-button>
			<u-button class="buttom-btn" type="primary" @click="submitForm" :loading="btnLoading">确 定</u-button>
		</view>
	</view>
</template>
<script>
    import { getDictionaryDataSelector,getDataInterfaceRes } from '@/api/common'
    import request from '@/utils/request'
    export default {
        data() {
            return {
                btnLoading: false,
                dataForm: {
                    id:'',
					year:undefined,
					enabledMark:1,
                },
                rules: {
					year:[
						{
							required:true,
							message:'请输入年度',
							trigger:'"blur"'
						},
						{
							pattern:/^\d+$/,
							message:'请输入正确的数字',
							trigger:'"blur"'
						},
					],
                },
				jurisdictionType:'',
            };
        },
		watch: {
        },
        onLoad(option) {
			this.jurisdictionType = option.jurisdictionType
			let _title = ''
			if(option.jurisdictionType =='btn_add'){
				_title = "新增"
            }
			if(option.jurisdictionType =='btn_edit'){
				_title = "编辑"
            }
			if(option.jurisdictionType =='btn_detail'){
				_title = ""
            }
            this.dataForm.id = option.id || ''
            uni.setNavigationBarTitle({
                title: _title
            })
            this.initData()
			this.selfInit();
        },
        onReady() {
            this.$refs.dataForm.setRules(this.rules);
        },
        methods: {
			selfInit(){
			},
			resetForm() {
				this.$refs.dataForm.resetFields();
				this.selfInit()
			},
            initData() {
				this.$store.commit('base/UPDATE_RELATION_DATA', {})
                if (this.dataForm.id) {
                    request({
                        url: '/api/system/QmsBaseIpqccalendar/' + this.dataForm.id,
                        method: 'get',
                    }).then(res => {
                        this.dataForm = res.data;
                    })
                }
            },
			submitForm() {
                this.$refs.dataForm.validate(valid => {
                    if (!valid) return
                    this.btnLoading = true
                    if (this.dataForm.id) {
                        request({
                            url: '/api/system/QmsBaseIpqccalendar/' + this.dataForm.id,
                            method: 'put',
                            data: this.dataForm,
                        }).then(res => {
                            uni.showToast({
                                title: res.msg,
                                complete: () => {
                                    setTimeout(() => {
                                        this.btnLoading = false
                                        uni.$emit('refresh')
                                        uni.navigateBack()
                                    }, 1500)
                                }
                            })
                        })
                    } else {
                        request({
                            url: '/api/system/QmsBaseIpqccalendar',
                            method: 'post',
                            data: this.dataForm,
                        }).then(res => {
                            uni.showToast({
                                title: res.msg,
                                complete: () => {
                                    setTimeout(() => {
                                        this.btnLoading = false
                                        uni.$emit('refresh')
                                        uni.navigateBack()
                                    }, 1500)
                                }
                            })
                        })
                    }
                });
            },
        }
    };
</script>

